<?php

namespace App\Dao;

use App\Model\ConsumerCouponBillsModel;

class ConsumerCouponBillsDao extends BaseDao
{
    public static function builder_query($params = []): \Illuminate\Database\Eloquent\Builder
    {
        $query = ConsumerCouponBillsModel::query();

        $query->when(checkIsHasField($params,'user_id'),function ($query)use ($params){
            $query->where('user_id',$params['user_id']);
        });



        return $query->orderByDesc('created_at');
    }


    public static function oli_consumer_coupon_update($user_id = 0,$nums = 0,$title = '')
    {
        UsersDao::builder_query([
            'user_id'=>$user_id
        ])->increment('consumer_coupon',$nums);
        //添加记录
        self::builder_query()->create([
            'user_id'=>$user_id,
            'nums'=>$nums,
            'title'=>$title,
            'balance'=>UsersDao::builder_query([
                'user_id'=>$user_id
            ])->value('consumer_coupon')
        ]);
    }

    public static function consumerCouponBills(array $params)
    {
        return self::builder_query($params)->get();
    }

    public static function pagenate(array $all)
    {
        return self::builder_query($all)->paginate();
    }
}
